package com.ae.shield.common.items;

import com.ae.shield.AEShieldMod;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.registries.ForgeRegistries;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

public class EffectStorageItem extends Item {
    public EffectStorageItem() {
        super(new Properties().group(AEShieldMod.AE_SHIELD));
    }

    @Override
    public void addInformation(@Nonnull ItemStack stack, @Nullable World worldIn, @Nonnull List<ITextComponent> tooltip, @Nonnull ITooltipFlag flagIn) {
        EffectInstance instance = readPotion(stack);
        ITextComponent effect;
        ITextComponent level = new StringTextComponent("0").mergeStyle(TextFormatting.BLUE);
        if (instance != null) {
            effect = ((TranslationTextComponent)instance.getPotion().getDisplayName()).mergeStyle(TextFormatting.BLUE);
            level = new StringTextComponent(String.valueOf(instance.getAmplifier() + 1)).mergeStyle(TextFormatting.BLUE);
        } else {
            effect = new TranslationTextComponent("tooltip.ae.shield.empty").mergeStyle(TextFormatting.BLUE);
        }
        tooltip.add(new TranslationTextComponent("tooltip.ae.shield.effect", effect, level).mergeStyle(TextFormatting.GRAY));
    }

    public void writePotion(ItemStack stack, EffectInstance effect) {
        CompoundNBT nbt = effect.write(new CompoundNBT());
        stack.getOrCreateTag().put("Effect", nbt);
    }

    @Nullable
    public EffectInstance readPotion(ItemStack stack) {
        CompoundNBT effect = readPotionNbt(stack);
        if (effect != null) {
            return EffectInstance.read(effect);
        }
        return null;
    }

    @Nullable
    public CompoundNBT readPotionNbt(ItemStack stack) {
        CompoundNBT compoundNBT = (CompoundNBT) stack.getOrCreateTag().get("Effect");
        if (compoundNBT != null) {
            return compoundNBT.copy();
        }
        return null;
    }
}
